//
//  BBTabBarController.swift
//  BusinessBao
//
//  Created by Chen on 2017/7/16.
//  Copyright © 2017年 Chen. All rights reserved.
//

import UIKit

class BBTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBar.isTranslucent = false
        self.navigationController?.setNavigationBarHidden(true, animated: true)

        let barItemTitles = [ "账本", "签署", "我的" ]
        let tabBarClasses: [UIViewController.Type] = [ BBBaseViewController.self,
                                                       BBBaseViewController.self,
                                                       BBBaseViewController.self ]
        let normalImages = [ "jb_tab_account_book_normal",
                             "jb_tab_sign_normal",
                             "jb_tab_mine_normal" ]
        let selectImages = [ "jb_tab_account_book_select",
                             "jb_tab_sign_select",
                             "jb_tab_mine_select" ]
        
        var navigations: Array<UIViewController> = []
        for i in 0..<tabBarClasses.count {
            let tabBarItem = createItem(barItemTitles[i],
                                        normalImage: normalImages[i],
                                        selectImage: selectImages[i])
            let controllerClass = tabBarClasses[i]
            let vc = controllerClass.init()
            vc.tabBarItem = tabBarItem
            vc.title = barItemTitles[i]
            
            let nav = BBNavigationController(rootViewController: vc)
            navigations.append(nav)
        }
        
        self.viewControllers = navigations
        
//        self.selectedIndex = 1
    }
    
    func createItem(_ title: String, normalImage: String, selectImage: String) -> UITabBarItem {
        let normalImage = UIImage(named: normalImage)?.withRenderingMode(.alwaysOriginal)
        let selectImage = UIImage(named: selectImage)?.withRenderingMode(.alwaysOriginal)
        
        let tabBarItem = UITabBarItem(title: title, image: normalImage, selectedImage: selectImage)
        tabBarItem.setTitleTextAttributes([ NSForegroundColorAttributeName : Nav_BG_Color ],
                                          for: UIControlState.selected)
        tabBarItem.setTitleTextAttributes([ NSForegroundColorAttributeName : Normal_Text_Color,
                                            NSFontAttributeName : Font(font: 11)],
                                          for: UIControlState.normal)
        return tabBarItem
    }
    
}
